package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.ParamMissException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.sky.constant.MessageConstant.DISH_ON_SALE;
import static com.sky.constant.MessageConstant.PARAMS_MISSED;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public PageResult findPage(SetmealPageQueryDTO pageQuery) {
        //1.分页条件构建
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());
        //2.构建查询条件并SQL查询
        if (StringUtils.isNotBlank(pageQuery.getName())){
            pageQuery.setName("%"+pageQuery.getName()+"%");
        }
        //sql执行 返回结果
        Page<SetmealVO> page = setmealMapper.findPage(pageQuery);
        //得到结果封装
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void insertService(SetmealDTO setmealDTO) {
        //1.新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //2.保存
        setmealMapper.insertService(setmeal);


        //获取id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());

        });
        setmealMapper.insertSetmealDish(setmealDishes);
    }

    @Override
    public void updateStatus(Integer status, long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);

        Setmeal build = setmeal.builder().status(status).id(id).build();
        setmealMapper.updateSetmeal(build);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        //1.校验参数
        if (ids.size()<=0){
            throw new DeletionNotAllowedException(PARAMS_MISSED);
        }
        //2.判断是否可删除
        int count=setmealMapper.selectStatusById(ids);
        if (count>0){
            throw new ParamMissException(DISH_ON_SALE);
        }
        setmealMapper.deleteByIds(ids);

    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //校验id不能为空
        Long id = setmealDTO.getId();
        if (id==null){
            throw new ParamMissException(PARAMS_MISSED);
        }
        //更新菜品
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.updateSetmeal(setmeal);

        //删除套餐和菜品的关系，操作setmeal_dish表
        setmealMapper.deleteSetmealDishById(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        if (setmealDishes!=null && setmealDishes.size()>0){
            setmealMapper.insertSetmealDish(setmealDishes);
        }
    }

    @Override
    public SetmealVO findById(Long id) {
        SetmealVO setmealVo=setmealMapper.findById(id);
        return setmealVo;
    }

    @Override
    public List<Setmeal> selectSetmealByCategoryId(String categoryId) {
        List<Setmeal> setmeal=setmealMapper.selectSetmealByCategoryId(categoryId);
        return setmeal;
    }

    @Override
    public List<DishItemVO> selectDishById(Long id) {
        List<DishItemVO> dishItemVOList=setmealMapper.selectDishById(id);
        return dishItemVOList;
    }
}
